
package com.idealighter.game.games.foursshop.struct;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Queues;

import com.idealighter.game.dictionary.domain.FoursLogPlanDomain;
import com.idealighter.game.games.common.AbstractTable;
import com.idealighter.game.games.foursshop.dto.BetsInfo;

import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ScheduledFuture;

/**
 * . 4s店桌子.
 * 
 * @date 2015年9月28日 下午2:22:45
 */
public class FoursShopTable extends AbstractTable {
  // 所属房间
  public final FoursShopRoom room;
  public List<FoursShopSeat> seats = Lists.newArrayList();
  // 当前游戏的倍率(所有汽车的倍率)
  public List<FoursLogPlanDomain> plans = Lists.newArrayList();
  // 上庄玩家(在队列的庄家)
  // public Map<Long, FoursShopSeat> bankers = Maps.newLinkedHashMap();
  public Queue<Long> bankers = Queues.newLinkedBlockingQueue(3);
  // 所有玩家总下注数（包含机器人）
  public long totalBet;
  // 所有机器人总下注数
  public long totalRobotBet;
  // 所有玩家总下注数
  public long totalplayerBet;
  // 当前开出的倍率
  private int rate;
  // 当前开出的汽车图标
  private int carId;
  // 开出的倍率
  public int temprate;
  // 开出的汽车图标
  public int tempcarId;
  // 游戏所属步骤(1:休息，2:下注，3:开奖,4:结算)
  public int state = 0;
  // 游戏步骤(休息、下注、开奖、结算)定时Future
  public ScheduledFuture<?> stepFuture = null;
  // 定时任务->定时推送总下注信息
  public ScheduledFuture<?> stepFuturePush = null;
  // 当前庄家Id 系统为0
  public long bankerPlayerId = 0;
  // 当前玩家人数
  public int playerNum = 0;
  // 房间玩家最大人数
  public final int playerMaxNum;
  // 彩金池收入(假)
  public long mosaicGold = 0;
  // 彩金池(真)
  public long realmosaicGold = 0;
  // 连续开出次数统计
  public int mosaicGoldCount = 0;
  // 爆出彩金数
  public int totalMosaic = 0;
  // 当前庄家局数
  public int count = 0;
  // 赢了多少
  public long score = 0;
  // 玩家总下注筹码信息(包含机器人)
  public Map<Integer, Long> betsTotal = Maps.newConcurrentMap();
  // 机器人总下注筹码信息
  public Map<Integer, Long> robotBetsTotal = Maps.newConcurrentMap();
  // 玩家总下注
  public Map<Integer, Long> playerBetsTotal = Maps.newConcurrentMap();
  public boolean isCancelBanker = false;
  // 统一开始位置
  public int startIndex = 0;
  // 统一开奖时间
  public int rewardTime = 0;
  // 系统收入
  public long earn = 0;

  public int getCarId() {
    return carId;
  }

  public void setCarId(int carId) {
    this.carId = carId;
  }

  public int getRate() {
    return rate;
  }

  /**
   * 获得倍率 .
   *
   * @param carId .
   * @return
   */
  public int getRate(int carId) {
    for (FoursLogPlanDomain domain : this.plans) {
      if (domain.getCarid() == carId) {
        return domain.getRate();
      }
    }
    return 1;
  }

  public void setRate(int rate) {
    this.rate = rate;
  }

  /**
   * 构造函数.
   * 
   * @param id .
   */
  public FoursShopTable(int id, FoursShopRoom room, int chairs) {
    super(id);
    this.room = room;
    this.playerMaxNum = chairs;
    for (int i = 1; i <= chairs; i++) {
      this.seats.add(new FoursShopSeat(i, this));
    }
  }

  /**
   * .
   */
  public void clear() {
    isCancelBanker = false;
    betsTotal.clear();
    robotBetsTotal.clear();
    playerBetsTotal.clear();
    totalBet = 0;
    totalRobotBet = 0;
    totalplayerBet = 0;

  }

  /**
   * 玩家总的下注信息 .
   *
   * @return
   */

  public List<BetsInfo> getTotalBetInfo() {
    List<BetsInfo> infos = Lists.newArrayList();
    for (Map.Entry<Integer, Long> entry : betsTotal.entrySet()) {
      BetsInfo info = new BetsInfo();
      info.setCarId(entry.getKey());
      info.setBet(entry.getValue());
      infos.add(info);
    }
    return infos;
  }

  /**
   * 玩家某个图标的下注 .
   *
   * @return
   */

  public long getTotalByCar() {
    long bet = 0;
    for (Map.Entry<Integer, Long> entry : betsTotal.entrySet()) {
      if (entry.getKey() == tempcarId) {
        bet += entry.getValue();
      }
    }
    return bet * temprate;
  }

  /**
   * 机器人某个图标总的下注信息(总和) . 下注区域*下注筹码
   *
   * @return
   */

  public long getRobotTotalBetInfo() {
    long bet = 0;
    for (Map.Entry<Integer, Long> entry : robotBetsTotal.entrySet()) {
      if (entry.getKey() == tempcarId) {
        bet += entry.getValue();
      }
    }
    return bet * temprate;
  }

  /**
   * 玩家某个图标总的下注信息(总和) . 下注区域*下注筹码
   *
   * @return
   */

  public long getPlayerTotalBetInfo() {
    long bet = 0;
    for (Map.Entry<Integer, Long> entry : playerBetsTotal.entrySet()) {
      if (entry.getKey() == tempcarId) {
        bet += entry.getValue();
      }
    }
    return bet * temprate;
  }

  /**
   * 机器人总的下注信息(总和) . 总下注筹码
   *
   * @return
   */
  public long getRobotTotal() {
    long bet = 0;
    for (Map.Entry<Integer, Long> entry : robotBetsTotal.entrySet()) {
      bet += entry.getValue();
    }
    return bet;
  }

  /**
   * 玩家总的下注信息(总和) . 总下注筹码
   *
   * @return
   */
  public long getPlayerTotal() {
    long bet = 0;
    for (Map.Entry<Integer, Long> entry : playerBetsTotal.entrySet()) {
      bet += entry.getValue();
    }
    return bet;
  }



  /**
   * 计算总筹码 .
   *
   * @param carId .
   * @return
   */
  public long getTotalBet(int carId) {
    return this.betsTotal.get(carId) != null ? this.betsTotal.get(carId) : 0;
  }

  /**
   * 庄家下注开奖阶段退出时扣除的筹码 .
   *
   * @return
   */
  public long getAllBets() {
    long totalBet = 0;
    for (Map.Entry<Integer, Long> entry : this.betsTotal.entrySet()) {
      int carId = entry.getKey();
      int rate = getRate(carId);
      long temp = entry.getValue() * rate;
      if (totalBet < temp) {
        totalBet = temp;
      }
    }
    return totalBet;
  }

  @SuppressWarnings("unchecked")
  @Override
  public FoursShopRoom room() {
    return room;
  }

  @Override
  public List<FoursShopSeat> seats() {

    return seats;
  }
}
